from fastapi import APIRouter, Depends, Path, HTTPException, UploadFile, File, Request
from typing import List
from service.sysJobService import SysJobService
from schema.sysJobSchema import SysJobCreate, SysJobUpdate, PageParams, SysJobRun, SysJobChange
from utils.response import ResponseUtil, ResponseModel
from urllib.parse import quote
from fastapi.responses import StreamingResponse
from datetime import datetime
from io import BytesIO

router = APIRouter(prefix="/sysJob", tags=["定时任务调度表"])
service = SysJobService()


@router.post(
    "",
    response_model=ResponseModel,
    summary="新增定时任务调度表",
    description="创建一个新的定时任务调度表。",
    response_description="返回创建成功的信息"
)
async def create(data: SysJobCreate, request: Request):
    """新增定时任务调度表"""
    current_user = request.state.user
    creator = current_user["username"]
    result = await service.create(data, creator)
    return ResponseUtil.success(data=result, msg="创建成功")


@router.get(
    "/get/{id}",
    response_model=ResponseModel,
    summary="获取定时任务调度表详情",
    description="根据ID获取定时任务调度表的详细信息",
    response_description="返回定时任务调度表的详细信息"
)
async def get_detail(id: int):
    """获取定时任务调度表详情"""
    result = await service.get_by_id(id)
    return ResponseUtil.success(data=result)


@router.patch(
    "/update/{id}",
    response_model=ResponseModel,
    summary="更新定时任务调度表",
    description="更新指定ID的定时任务调度表信息。支持部分字段更新。",
    response_description="返回更新后的信息"
)
async def update(id: int, data: SysJobUpdate, request: Request):
    """更新定时任务调度表"""
    current_user = request.state.user
    updater = current_user["username"]
    result = await service.update(id, data, updater, request)
    return ResponseUtil.success(data=result, msg="更新成功")


@router.delete(
    "/batch/{ids}",
    response_model=ResponseModel,
    summary="批量删除定时任务调度表",
    description="根据ID删除定时任务调度表，支持多个ID(用逗号分隔)。此操作不可恢复，请谨慎操作。",
    response_description="返回成功删除的数量"
)
async def delete(request: Request, ids: str = Path(..., description="ID，多个用逗号分隔，如：1,2,3")):
    """批量删除定时任务调度表"""
    try:
        id_list = [int(id.strip()) for id in ids.split(",")]
    except ValueError:
        raise ResponseUtil.error(code=400, msg="无效的ID格式")

    current_user = request.state.user
    updater = current_user["username"]
    count = await service.delete(id_list, updater, request)
    return ResponseUtil.success(data={"count": count}, msg=f"成功删除{count}条数据")


@router.get(
    "/page",
    response_model=ResponseModel,
    summary="分页查询定时任务调度表列表",
    description="分页获取定时任务调度表列表，支持多条件查询",
    response_description="返回分页数据，包含列表数据、总数、页码、每页大小"
)
async def get_page(
    params: PageParams = Depends()
):
    """分页获取定时任务调度表列表"""
    records, total = await service.get_page(params)
    return ResponseUtil.success(data={
        "records": records,
        "total": total,
        "page": params.page_num,
        "limit": params.page_size
    })


@router.get(
    "/export",
    summary="导出定时任务调度表数据",
    description="导出所有定时任务调度表数据到Excel文件",
    response_description="返回Excel文件"
)
async def export_data():
    """导出数据到Excel"""
    excel_data = await service.export_excel()

    filename = f"定时任务调度表数据_{datetime.now().strftime('%Y%m%d%H%M%S')}.xlsx"
    encoded_filename = quote(filename)

    headers = {
        'Content-Disposition': f'attachment; filename="{encoded_filename}"; filename*=utf-8\'\'{encoded_filename}',
        'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }

    return StreamingResponse(
        BytesIO(excel_data.getvalue()),
        headers=headers,
        media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    )


@router.get(
    "/importTemplate",
    summary="下载定时任务调度表导入模板",
    description="下载定时任务调度表导入的Excel模板文件",
    response_description="返回Excel模板文件"
)
async def get_import_template():
    """获取导入模板"""
    template_data = service.get_import_template()
    filename = f"定时任务调度表导入模板_{datetime.now().strftime('%Y%m%d%H%M%S')}.xlsx"
    encoded_filename = quote(filename)

    headers = {
        'Content-Disposition': f'attachment; filename="{encoded_filename}"; filename*=utf-8\'\'{encoded_filename}',
        'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }

    return StreamingResponse(
        BytesIO(template_data.getvalue()),
        headers=headers,
        media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    )


@router.post(
    "/import",
    response_model=ResponseModel,
    summary="导入定时任务调度表数据",
    description="通过Excel文件批量导入定时任务调度表数据",
    response_description="返回导入结果，包括成功数量和错误信息"
)
async def import_data(
    file: UploadFile = File(..., description="Excel文件")
):
    """导入数据"""
    if not file.filename.endswith(('.xls', '.xlsx')):
        return ResponseUtil.error(msg="只支持Excel文件格式（.xls, .xlsx）")

    creator = "admin"

    try:
        success_count, error_msgs = await service.import_data(file, creator)
        return ResponseUtil.success(data={
            "successCount": success_count,
            "errorMsgs": error_msgs
        }, msg=f"成功导入{success_count}条数据")
    except HTTPException as e:
        return ResponseUtil.error(msg=str(e.detail))
    except Exception as e:
        return ResponseUtil.error(msg=f"导入失败: {str(e)}")


@router.patch(
    "/run",
    response_model=ResponseModel,
    summary="定时任务调度一次",
    description="执行一次定时任务",
    response_description="返回信息"
)
async def run_once(data: SysJobRun, request: Request):
    """定时任务调度一次"""
    current_user = request.state.user
    updater = current_user["username"]
    result = await service.execute_job(data.job_id)
    return ResponseUtil.success(data=result, msg="执行成功")


@router.patch(
    "/changeStatus",
    response_model=ResponseModel,
    summary="修改定时任务状态",
    description="修改定时任务状态",
    response_description="返回信息"
)
async def change_status(data: SysJobChange, request: Request):
    """修改定时任务状态"""
    current_user = request.state.user
    updater = current_user["username"]
    result = await service.change_status(data, updater, request)
    return ResponseUtil.success(data=result, msg="执行成功")
